/*
 * Worker.cpp
 *
 *  Created on: Sep 13, 2013
 *      Author: alexandru
 */

#include "Worker.h"

namespace stn {













Worker::Worker(std::string configFile) {
	using namespace std;





cout << "WORKER CONFIG FILE: " << configFile << endl;





	 std::ifstream o;
	 std::string line;
	 string res;

	 o.open(configFile.c_str(), ios::in);
	 if (o.is_open()) {
		 while ( getline(o, line) ){
			 if(line.at(0)!=UTILS_SHARP_KEY){
				 res+=line+NEWLINE;
			 }

		 }
	 }
	 o.close();




	 vector<SimpleStruct> datas=Utils::convertToSimpleStruct(res);


	 for(int i=0; i< (int) datas.size(); i++){
		 cout << datas.at(i).property << " - has value - " <<datas.at(i).value << endl;

		 if(datas.at(i).property==CONFIG_PASSWORD){
			 password=datas.at(i).value;
		 }

		 if(datas.at(i).property==LAST_USED_DATABASE){
			 lastUsedDatabase=datas.at(i).value;
		 }

		 if(datas.at(i).property==WORKING_DIR){
		 			 workingDir=datas.at(i).value;
		 		 }
	 }


	 cout << CONFIG_PASSWORD<< " : "<< password << endl;
	 cout << WORKING_DIR << " : "<< workingDir << endl;


	 Object newObj("root");
	 newObj.setWorkingDir(workingDir);
	 newObj.isRoot=true;
	 newObj.importLocalXml("xmlsample.xml", true);
//newObj.importLocalXml("test.xml", true);
	// newObj.importRemoteXml("http://alex2.cam4.com/");
	 //newObj.runCommand("setName(newNameHere)");
	// newObj.importFolderStruct("/Users/alexandru/Documents/DEV_TOOLSs\\");

	 cout << "JSON BASE: "<< newObj.toJson() << endl;
	 cout << "JSON BEAUTY: \n " << endl;
	 cout << newObj.toJsonBeauty("") << endl;
//	 cout << newObj.toXML() << endl;
//	 cout << newObj.toXMLBeauty() << endl;
//	 cout << newObj.getJsonValue()<< endl;
	 std::exit(0);

	 			string str;
	 			while (str!=FUNC_EXIT){
	 				std::getline (std::cin,str);
	 				cout << newObj.runCommand(str)  << endl;
	 				cout << newObj.toJson() << endl;
	 			}


	// TODO Auto-generated constructor stub




/*

	Object root("root");


		Object * charObj=new Object("charName", "charVal");

		Object bol("bol", true);

		Object num("num", 4);

		Object bol2("bol2", false);
		Object num2("num2", 5);

		Object bol3("bol2", false);
			Object num3("num3", 5);


		Object a("a");
		Object b("b");
		Object c("c");


	a.push(charObj);//0
	a.addObject(bol);//1
	a.addObject(num);//2


	b.addObject(bol2);//0
	b.addObject(num2);//1

	c.addObject(bol3);
	c.addObject(num3);

	b.addObject(c);//2

	a.addObject(b);//3
	//a.addObject(b);

	//b.addObject(c);

	root.setType(5);
		root.addObject(a);
*/

//	Object newObj("sample");
//			newObj.isRoot=true;
			//newObj.setValue("{str:\"stringName\", \"object\": {\"char\":\"name\", \"arr\":[1,2,3,true,5, {\"name\":\"nume\", prop:45, bol:false}]}}");
			//newObj.setValue("[1, 2, 3, true, 5, {\"name\":\"name\"}]");

			//cout << newObj.runCommand("#str") << endl;




//			ifstream fin;                          // Declaring an input stream object
//			fin.open("Bookmarks", ios::in);
//			string outPut;
//			string res;

//			if(fin.is_open()){
//				while (getline(fin, outPut)) {
//								//cout << outPut << endl;
//								res+=outPut;
//							}
//			}
//			newObj.setValue(res);
	               //

	               // return 1;
			//cout << newObj.toJson()  << endl;
			//cout << newObj.runCommand("[0][1][2][3][2][0]")  << endl;//MERGE


			//cout << newObj.runCommand("obiect[1]list[3][2]altaProprietate")  << endl;//MERGE

			//cout << newObj.runCommand("obiect.objectChild.list[3]one=\"someString\"")  << endl;//MERGE
			//cout << newObj.runCommand("obiect=\"someString\"")  << endl;//MERGE
			//cout << newObj.runCommand("[0] = false")  << endl;//MERGE
			//cout << newObj.runCommand("[2]+  [1, 3, 2, 4]     ")  << endl;//MERGE
			//cout << newObj.runCommand("jsonize")  << endl;//MERGE
	//		cout << newObj.runCommand("+{\"prop\":false}") ;
	//		newObj.saveToFile();
			//cout << newObj.runCommand("+{\"prop\":false}") ;

			//newObj.getInstanceByName("list")->removeAtIndex(1);//MERGE
			//cout << newObj.getByName("list").toJson() << endl;

	//



	//		cout << newObj.runCommand("obiect={\"unu\":\"unu\", \"doi\":\"doi\"}")  << endl;//MERGE
			//cout << newObj.runCommand("+(\"name\"   :   {   \"unu\":\"unu\"  ,    \"doi\": \"doi\"}      )")  << endl;//MERGE


	//cout << newObj.runCommand("a=4") << endl;
	//cout << newObj.runCommand("B+4") << endl;
	//cout << newObj.runCommand("B+5") << endl;

	//		cout << Utils::isLikeWildcard("ANTEunua", "*unua") << endl;
	//		cout << Utils::isLikeWildcard("unuPOST", "unu*") << endl;
	//		cout << Utils::isLikeWildcard("ANTEunuPOST", "*unu*") << endl;
	//		cout << Utils::isLikeWildcard("unu", "unu") << endl;
	//		return 5;

//	              cout <<  newObj.runQuery("UPDATE someText VALUES EQ 5")->toJson() << endl;
//	              cout << newObj.toJson() << endl;
				//return 4;
	//		inlineTest:
	//
			//newObj.loadFromFile();
//			string str;
//			while (str!="exit"){
//				std::getline (std::cin,str);
//				cout << newObj.runCommand(str)  << endl;
//				cout << newObj.toJson() << endl;
//			}




			//cout << newObj.toJson() << endl;

}

Worker::~Worker() {

	std::cout << "bye!" << std::endl;
}

} /* namespace stn */
